package org.iabako.client.ui.page.home;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.page.business.client.ClientSearch;
import org.iabako.client.ui.page.business.expense.ExpenseSearch;
import org.iabako.client.ui.page.business.sale.PastInstallmentSearch;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.enumeration.FinancialStatusClient;
import org.iabako.shared.iservice.business.FinanceServiceAsync;
import org.iabako.shared.querydefinition.columndefinition.ClientColumnEnum;
import org.iabako.shared.querydefinition.columndefinition.ExpenseColumnEnum;
import org.iabako.shared.querydefinition.dto.QueryDefinitionDTO;
import org.iabako.shared.querydefinition.dto.QueryOperatorEnum;
import org.iabako.shared.tools.GenericTools;

import java.util.List;

/**
 * Created by jose on 20/05/15.
 */
public class WhatsNewPanel extends Composite {

    interface MyUiBinder extends UiBinder<Panel, WhatsNewPanel> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

    protected final FinanceServiceAsync financeServiceAsync = FinanceServiceAsync.Util.getInstance();

    @UiField
    LabelText numberClientsWithPastInstallments;
    @UiField
    LabelText totalPastInstallments;
    @UiField
    LabelText totalPendingExpenses;
    @UiField
    HTMLPanel financialTables;
    @UiField
    HTMLPanel colorLabelsPanel;
    @UiField
    LabelText numberClientsWithPastInstallmentsLabel;
    @UiField
    LabelText totalPastInstallmentsLabel;
    @UiField
    LabelText totalPendingExpensesLabel;
    @UiField
    HTMLPanel bigResumePanel;
    @UiField
    HTMLPanel financialTablesPanel;
    @UiField
    HTMLPanel waitIconPanel;

    public WhatsNewPanel() {
        initWidget(binder.createAndBindUi(this));

        if (!AuthenticateUserProvider.getUser().isNoFinancialAccess()) {
            colorLabelsPanel.setVisible(true);
            financialTables.setVisible(true);
        }

        showResumeInfo();
    }

    private void showResumeInfo() {
        financeServiceAsync.getPendingResume(new IabakoAsyncCallback<List<Double>>() {
            public void onFailure(Throwable caught) {
                super.onFailure(caught);
                //TODO something else
            }

            public void onSuccess(List<Double> result) {
                numberClientsWithPastInstallments.setLabel(result.get(0).intValue() + "");
                numberClientsWithPastInstallments.getLabel().addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        ClientSearch clientSearch = new ClientSearch() {
                            @Override
                            protected QueryDefinitionDTO getQueryDefinitionDTO() {
                                QueryDefinitionDTO queryDefinitionDTO = new QueryDefinitionDTO();
                                queryDefinitionDTO.addCriteria(ClientColumnEnum.status, QueryOperatorEnum.EQUALS, FinancialStatusClient._1_inDebt);
                                return queryDefinitionDTO;
                            }
                        };

                        clientSearch.clientSearchForm.setPastInstallmentsCriteria();
                        NavigationController.getInstance().goTo(clientSearch);
                    }
                });
                totalPastInstallments.setLabel(GenericTools.formatAmount(result.get(1) + ""));
                totalPastInstallments.getLabel().addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        PastInstallmentSearch pastInstallmentSearch = new PastInstallmentSearch();
                        NavigationController.getInstance().goTo(pastInstallmentSearch);
                    }
                });

                totalPendingExpenses.setLabel(GenericTools.formatAmount(result.get(2) + ""));
                totalPendingExpenses.getLabel().addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent event) {
                        ExpenseSearch expenseSearch = new ExpenseSearch() {
                            @Override
                            protected QueryDefinitionDTO getQueryDefinitionDTO() {
                                QueryDefinitionDTO queryDefinitionDTO = new QueryDefinitionDTO();
                                queryDefinitionDTO.addCriteria(ExpenseColumnEnum.paymentMethod, QueryOperatorEnum.ISNULL, null);
                                queryDefinitionDTO.addCriteria(ExpenseColumnEnum.paymentDate, QueryOperatorEnum.LESS_THAN_OR_EQUALS, GenericTools.getDateDay());
                                return queryDefinitionDTO;
                            }
                        };
                        expenseSearch.expenseSearchForm.setPendingExpenseCriteria();
                        NavigationController.getInstance().goTo(expenseSearch);
                    }
                });

                if (AuthenticateUserProvider.getUser().isNoFinancialAccess()) {
                    renderBigResume();
                }

                financialTablesPanel.setVisible(true);
                waitIconPanel.setVisible(false);

                super.onSuccess(result);
            }
        });
    }

    private void renderBigResume() {
        HorizontalPanel horizontalPanel = new HorizontalPanel();
        horizontalPanel.setStyleName("homebigresumetables");
        horizontalPanel.add(numberClientsWithPastInstallments);
        horizontalPanel.add(numberClientsWithPastInstallmentsLabel);
        numberClientsWithPastInstallments.setStyleClass("resumetableamount resumetableamountbig");
        bigResumePanel.add(horizontalPanel);

        horizontalPanel = new HorizontalPanel();
        horizontalPanel.setStyleName("homebigresumetables");
        totalPastInstallmentsLabel.setLabel(totalPastInstallmentsLabel.getLabelText().replace(":", ""));
        horizontalPanel.add(totalPastInstallments);
        horizontalPanel.add(totalPastInstallmentsLabel);
        totalPastInstallments.setStyleClass("resumetableamount resumetableamountbig");
        bigResumePanel.add(horizontalPanel);

        horizontalPanel = new HorizontalPanel();
        horizontalPanel.setStyleName("homebigresumetables");
        totalPendingExpensesLabel.setLabel(totalPendingExpensesLabel.getLabelText().replace(":", ""));
        horizontalPanel.add(totalPendingExpenses);
        horizontalPanel.add(totalPendingExpensesLabel);
        totalPendingExpenses.setStyleClass("resumetableamount resumetableamountbig");
        bigResumePanel.add(horizontalPanel);

        bigResumePanel.setVisible(true);
    }
}
